package com.heima.kafka.controller;

import com.alibaba.fastjson.JSON;
import com.heima.kafka.pojos.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 黄晏凯
 */
@RestController
public class HelloController {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @GetMapping("/send")
    public String hello() {
        kafkaTemplate.send("itcast-topic", "hyk最NB");
        return "success";
    }

    @GetMapping("/sendObj")
    public String sendObj() {
        User user = new User();
        user.setUsername("hyk");
        user.setAge(23);
        kafkaTemplate.send("user-topic", JSON.toJSONString(user));
        return "success";
    }
}